package com.mictapp.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.mictapp.domain.Patient;

public class XMLUtil {
	// help to parser XML
		public static List<Patient> parseXMLData(InputStream is) {
			List<Patient> result = null;

			try {
				XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
				factory.setNamespaceAware(true);
				XmlPullParser xpp = factory.newPullParser();
				xpp.setInput(is, "UTF-8");
				int eventType = xpp.getEventType();

				Patient po = null;
				while (eventType != XmlPullParser.END_DOCUMENT) {

					switch (eventType) {
					case XmlPullParser.START_TAG:
						if (xpp.getName().equals("Patients")) {
							result = new ArrayList<Patient>();
						} else if (xpp.getName().equals("Patient")) {
							po = new Patient();
						} else if (xpp.getName().equals("pId")) {
							po.setFpid(xpp.nextText());
						} else if (xpp.getName().equals("pName")) {
							po.setFname(xpp.nextText());
						} else if (xpp.getName().equals("pSex")) {
							po.setFsex(xpp.nextText());
						}else if(xpp.getName().equals("pGroup")){
							po.setFgroup(xpp.nextText());
						}else if (xpp.getName().equals("pPhone")) {
							po.setFphone1(xpp.nextText());
						}else if (xpp.getName().equals("pEndTime")) {
							po.setFedateStr(xpp.nextText());
						}else if (xpp.getName().equals("pBeginTime")) {
							po.setFbdateStr(xpp.nextText());
						}
						break;
					case XmlPullParser.END_TAG:
						if (xpp.getName().equals("Patient")) {
							result.add(po);
						}
						break;
					case XmlPullParser.TEXT:
						break;
					}
					eventType = xpp.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;
		}

}
